<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>【CSS杂谈】妙用 background 实现花式文字效果 | Vixcity</title><meta name="keywords" content="CSS"><meta name="author" content="Vixcity"><meta name="copyright" content="Vixcity"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="【CSS杂谈】妙用 background 实现花式文字效果Hello 大家好，我是 Vixcity。本文将讲解如何利用 background 系列属性，巧妙的实现一些花式的文字效果。通过本文，你将可以学到：  通过 background-size 与 background-position 实现酷炫的文字下划线效果 通过 background-size 与 background-position">
<meta property="og:type" content="article">
<meta property="og:title" content="【CSS杂谈】妙用 background 实现花式文字效果">
<meta property="og:url" content="http://vixcity.gitee.io/my-gitee-blog/2021/09/16/%E3%80%90CSS%E3%80%91%E5%A6%99%E7%94%A8%20background%20%E5%AE%9E%E7%8E%B0%E8%8A%B1%E5%BC%8F%E6%96%87%E5%AD%97%E6%95%88%E6%9E%9C/index.html">
<meta property="og:site_name" content="Vixcity">
<meta property="og:description" content="【CSS杂谈】妙用 background 实现花式文字效果Hello 大家好，我是 Vixcity。本文将讲解如何利用 background 系列属性，巧妙的实现一些花式的文字效果。通过本文，你将可以学到：  通过 background-size 与 background-position 实现酷炫的文字下划线效果 通过 background-size 与 background-position">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://vixcity.gitee.io/my-gitee-blog/img/CSS.jpg">
<meta property="article:published_time" content="2021-09-15T16:00:00.000Z">
<meta property="article:modified_time" content="2022-01-04T03:04:47.316Z">
<meta property="article:author" content="Vixcity">
<meta property="article:tag" content="CSS">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://vixcity.gitee.io/my-gitee-blog/img/CSS.jpg"><link rel="shortcut icon" href="/my-gitee-blog/img/favicon.ico"><link rel="canonical" href="http://vixcity.gitee.io/my-gitee-blog/2021/09/16/%E3%80%90CSS%E3%80%91%E5%A6%99%E7%94%A8%20background%20%E5%AE%9E%E7%8E%B0%E8%8A%B1%E5%BC%8F%E6%96%87%E5%AD%97%E6%95%88%E6%9E%9C/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//fonts.googleapis.com" crossorigin=""/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/my-gitee-blog/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Titillium+Web&amp;display=swap" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/my-gitee-blog/',
  algolia: undefined,
  localSearch: {"path":"/my-gitee-blog/search.xml","languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: {"defaultEncoding":1,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"简"},
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: '/my-gitee-blog/',
      css: '/my-gitee-blog/'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '【CSS杂谈】妙用 background 实现花式文字效果',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: false,
  postUpdate: '2022-01-04 11:04:47'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><link rel="stylesheet" href="/my-gitee-blog/css/mycss.css"><link rel="stylesheet" href="/my-gitee-blog/css/newStyle.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/font-awesome-animation@1.1.1/css/font-awesome-animation.css"><link rel="stylesheet" href="https://unpkg.com/swiper/swiper-bundle.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Zfour/Butterfly-card-history/baiduhistory/css/main.css"><!-- hexo injector head_end start --><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Zfour/hexo-electric-clock@1.0.6/clock.css"><link rel="stylesheet" href="https://unpkg.zhimg.com/hexo-butterfly-wowjs/lib/animate.min.css" media="print" onload="this.media='screen'"><!-- hexo injector head_end end --><meta name="generator" content="Hexo 5.4.0"></head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">加载中...</div></div></div><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/my-gitee-blog/img/avatar.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/my-gitee-blog/archives/"><div class="headline">文章</div><div class="length-num">55</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/my-gitee-blog/tags/"><div class="headline">标签</div><div class="length-num">50</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/my-gitee-blog/categories/"><div class="headline">分类</div><div class="length-num">18</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/my-gitee-blog/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/PicBed"><i class="fa-fw fas fa-image"></i><span> 图库</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/about/"><i class="fa-fw fas fa-heart"></i><span> 关于我</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('/my-gitee-blog/img/CSS.jpg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/my-gitee-blog/">Vixcity</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/my-gitee-blog/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/PicBed"><i class="fa-fw fas fa-image"></i><span> 图库</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/about/"><i class="fa-fw fas fa-heart"></i><span> 关于我</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">【CSS杂谈】妙用 background 实现花式文字效果</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2021-09-15T16:00:00.000Z" title="发表于 2021-09-16 00:00:00">2021-09-16</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2022-01-04T03:04:47.316Z" title="更新于 2022-01-04 11:04:47">2022-01-04</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/my-gitee-blog/categories/%E5%A4%A7%E5%89%8D%E7%AB%AF/">大前端</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/my-gitee-blog/categories/%E5%A4%A7%E5%89%8D%E7%AB%AF/CSS/">CSS</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">2.9k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>12分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="【CSS杂谈】妙用 background 实现花式文字效果"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h2 id="【CSS杂谈】妙用-background-实现花式文字效果"><a href="#【CSS杂谈】妙用-background-实现花式文字效果" class="headerlink" title="【CSS杂谈】妙用 background 实现花式文字效果"></a>【CSS杂谈】妙用 background 实现花式文字效果</h2><p>Hello 大家好，我是 Vixcity。本文将讲解如何利用 <code>background</code> 系列属性，巧妙的实现一些花式的文字效果。通过本文，你将可以学到：</p>
<ul>
<li>通过 <code>background-size</code> 与 <code>background-position</code> 实现酷炫的文字下划线效果</li>
<li>通过 <code>background-size</code> 与 <code>background-position</code> 以及 <code>background-clip</code> 实现文字逐个渐现的效果</li>
</ul>
<h2 id="起因"><a href="#起因" class="headerlink" title="起因"></a>起因</h2><p>写本文的动机是在于，某天，被这样一个标题所吸引 – 10 <a target="_blank" rel="noopener" href="https://medium.com/@MonteVerdeVT/10-masterfully-designed-websites-f999112e2fa9">Masterfully Designed Websites</a>，其中列举了 10 个极具创意的 Web 网站</p>
<p>其中一个 <a target="_blank" rel="noopener" href="https://thenewmobileworkforce.imm-g-prod.com/back-at-hq">Red Bull Racing</a> 网站，是介绍关于 F1 红牛车队的主页。其中有这样一个非常有意思的 Hover 动画效果：</p>
<p><img src="https://cdn.jsdelivr.net/gh/Vixcity/FigureBed/img/202109161115310.gif" alt="图片"></p>
<p>这个文字的 hover 出现效果，看似简单，其实想要完全实现它，仅仅依靠 CSS 是非常复杂的</p>
<p>其中一个比较难的地方在于 – <strong>如何让一个效果，逐渐作用给整段文字中的部分</strong></p>
<p>而不是一次将整个效果赋予整段文本</p>
<h2 id="利用-background-实现文字的下划线效果"><a href="#利用-background-实现文字的下划线效果" class="headerlink" title="利用 background 实现文字的下划线效果"></a>利用 background 实现文字的下划线效果</h2><p>到这里，我想起了之前在这篇文章中 – <a target="_blank" rel="noopener" href="https://github.com/chokcoco/iCSS/issues/103">CSS 文字装饰 text-decoration &amp; text-emphasis</a>，我介绍的一种 <strong>使用 background 模拟下划线</strong> 的效果</p>
<p>看个简单的 DEMO，使用 <code>background</code> 模拟文字的下划线效果：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">p</span>&gt;</span></span><br><span class="line">  测试测试测试测试测试测试测试测试测试测试测试测试测试测试<span class="tag">&lt;<span class="name">a</span>&gt;</span>测试测试测试测测试测试测试测试测试测试测试测试测试测试测试测试试测试测试<span class="tag">&lt;/<span class="name">a</span>&gt;</span>测试测试</span><br><span class="line"><span class="tag">&lt;/<span class="name">p</span>&gt;</span> </span><br></pre></td></tr></table></figure>

<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-tag">p</span>&#123;</span><br><span class="line">  <span class="attribute">width</span>: <span class="number">600px</span>;</span><br><span class="line">  <span class="attribute">font-size</span>: <span class="number">24px</span>;</span><br><span class="line">  <span class="attribute">color</span>: <span class="number">#666</span>;</span><br><span class="line">  <span class="attribute">word-wrap</span>: break-word;</span><br><span class="line">&#125;</span><br><span class="line"><span class="selector-tag">a</span>&#123;</span><br><span class="line">  <span class="attribute">background</span>: <span class="built_in">linear-gradient</span>(<span class="number">90deg</span>,<span class="number">#0cc</span>,<span class="number">#0cc</span>);</span><br><span class="line">  <span class="attribute">background-size</span>: <span class="number">100%</span> <span class="number">3px</span>;</span><br><span class="line">  <span class="attribute">background-repeat</span>: no-repeat;</span><br><span class="line">  <span class="attribute">background-position</span>: <span class="number">100%</span> <span class="number">100%</span>;</span><br><span class="line">  <span class="attribute">color</span>: <span class="number">#0cc</span>;</span><br><span class="line"> &#125;</span><br></pre></td></tr></table></figure>

<p>使用 <code>background</code> 模拟文字的下划线效果，效果图如下：</p>
<p><img src="https://cdn.jsdelivr.net/gh/Vixcity/FigureBed/img/202109161113708.jpg" alt="图片"></p>
<p>又或者，使用 <code>background</code> 模拟虚线下划线：</p>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-tag">a</span>&#123;</span><br><span class="line">  <span class="comment">/* background: linear-gradient(90deg,#0cc,#0cc);</span></span><br><span class="line"><span class="comment">  background-size: 100% 3px;</span></span><br><span class="line"><span class="comment">  background-repeat: no-repeat;</span></span><br><span class="line"><span class="comment">  background-position: 100% 100%;</span></span><br><span class="line"><span class="comment">  color: #0cc; */</span></span><br><span class="line">  </span><br><span class="line">  <span class="attribute">background</span>: <span class="built_in">linear-gradient</span>(<span class="number">90deg</span>, <span class="number">#0cc</span> <span class="number">50%</span>, transparent <span class="number">50%</span>, transparent <span class="number">1px</span>);</span><br><span class="line">  <span class="attribute">background-size</span>: <span class="number">10px</span> <span class="number">2px</span>;</span><br><span class="line">  <span class="attribute">background-repeat</span>: repeat-x;</span><br><span class="line">  <span class="attribute">background-position</span>: <span class="number">100%</span> <span class="number">100%</span>;</span><br><span class="line">  <span class="attribute">color</span>: <span class="number">#0cc</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><img src="https://cdn.jsdelivr.net/gh/Vixcity/FigureBed/img/202109161112407.jpg" alt="图片"></p>
<p><a target="_blank" rel="noopener" href="https://codepen.io/Chokcoco/pen/YzNQKwm">使用 background 模拟下划线与虚线下划线</a></p>
<p>当然这个是最基础的，巧妙的运用 <code>background</code> 的各种属性，我们实现各种有意思的效果</p>
<h3 id="巧妙改变-background-size-与-background-position-实现文字-hover-动效"><a href="#巧妙改变-background-size-与-background-position-实现文字-hover-动效" class="headerlink" title="巧妙改变 background-size 与 background-position 实现文字 hover 动效"></a>巧妙改变 <code>background-size</code> 与 <code>background-position</code> 实现文字 hover 动效</h3><p>这里，通过巧妙改变 <code>background-size</code> 与 <code>background-position</code> 属性</p>
<p>我们可以实现一些非常有意思的文字 hover 效果</p>
<p>先看这样一个 Demo，核心代码作用于被 <code>&lt;p&gt;</code> 标签包裹的 <code>&lt;a&gt;</code> 标签之上：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">p</span>&gt;</span></span><br><span class="line">  测试测试测试测试测试测试测试测试测试测试测试测试测试测试<span class="tag">&lt;<span class="name">a</span>&gt;</span>测试测试测试测测试测试测试测试测试测试测试测试测试测试测试测试试测试测试<span class="tag">&lt;/<span class="name">a</span>&gt;</span>测试测试</span><br><span class="line"><span class="tag">&lt;/<span class="name">p</span>&gt;</span> </span><br></pre></td></tr></table></figure>

<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-tag">a</span> &#123;</span><br><span class="line">  <span class="attribute">background</span>: <span class="built_in">linear-gradient</span>(<span class="number">90deg</span>, <span class="number">#ff3c41</span>, <span class="number">#fc0</span>, <span class="number">#0ebeff</span>);</span><br><span class="line">  <span class="attribute">background-size</span>: <span class="number">0</span> <span class="number">3px</span>;</span><br><span class="line">  <span class="attribute">background-repeat</span>: no-repeat;</span><br><span class="line">  <span class="attribute">background-position</span>: <span class="number">0</span> <span class="number">100%</span>;</span><br><span class="line">  <span class="attribute">transition</span>: all <span class="number">1s</span>;</span><br><span class="line">  <span class="attribute">color</span>: <span class="number">#0cc</span>;</span><br><span class="line">  </span><br><span class="line">  <span class="comment">/* 2.0</span></span><br><span class="line"><span class="comment">  background: linear-gradient(90deg,#0cc,#0cc);</span></span><br><span class="line"><span class="comment">  background-size: 100% 3px;</span></span><br><span class="line"><span class="comment">  background-repeat: no-repeat;</span></span><br><span class="line"><span class="comment">  background-position: 100% 100%;</span></span><br><span class="line"><span class="comment">  color: #0cc; */</span></span><br><span class="line">  </span><br><span class="line">  <span class="comment">/* 1.0</span></span><br><span class="line"><span class="comment">  background: linear-gradient(90deg, #0cc 50%, transparent 50%, transparent 1px);</span></span><br><span class="line"><span class="comment">  background-size: 10px 2px;</span></span><br><span class="line"><span class="comment">  background-repeat: repeat-x;</span></span><br><span class="line"><span class="comment">  background-position: 100% 100%;</span></span><br><span class="line"><span class="comment">  color: #0cc; */</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="selector-tag">a</span><span class="selector-pseudo">:hover</span> &#123;</span><br><span class="line">  <span class="attribute">background-size</span>: <span class="number">100%</span> <span class="number">3px</span>;</span><br><span class="line">  <span class="attribute">color</span>: <span class="number">#000</span>;</span><br><span class="line"> &#125;</span><br></pre></td></tr></table></figure>

<p>我们虽然设定了 <code>background: linear-gradient(90deg, #ff3c41, #fc0, #0ebeff)</code>，但是一开始默认它的 <code>background-size: 0 3px</code>，也就是一开始是看不到下划线的，当 hover 的时候，改变 <code>background-size: 100% 3px</code>，这个时候，就会有一个 <code>0 3px</code> 到 <code>100% 3px</code> 的变换，也就是一个从无到有的伸展效果。</p>
<p>看看最后的效果（别人效果图）：</p>
<p><img src="https://cdn.jsdelivr.net/gh/Vixcity/FigureBed/img/202109161121838.gif" alt="图片"></p>
<p>由于设定的 <code>background-position</code> 是 <code>0 100%</code>，如果，设定的 <code>background-position</code> 是 <code>100% 100%</code>，我们可以得到一个反向的效果：  </p>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">// 其他都保持一致，只改变 <span class="attribute">background-position</span>，从 <span class="number">0</span> <span class="number">100%</span> 改为 <span class="number">100%</span> <span class="number">100%</span>  </span><br><span class="line"><span class="selector-tag">a</span> &#123;</span><br><span class="line">    ···  </span><br><span class="line">    <span class="attribute">background-position</span>: <span class="number">100%</span> <span class="number">100%</span>;</span><br><span class="line"> ···</span><br><span class="line">&#125;  </span><br></pre></td></tr></table></figure>

<p>再看看效果，你可以对比着上面的动图看看具体的差异点在哪：</p>
<p><img src="https://cdn.jsdelivr.net/gh/Vixcity/FigureBed/img/202109161125428.gif" alt="图片"></p>
<p><a target="_blank" rel="noopener" href="https://codepen.io/Chokcoco/pen/QWdgLwp">background underline animation</a></p>
<p>OK，如果我们使用 <code>background</code> 实现两条重叠的下划线，再利用上述的两个不同的 <code>background-position</code> 值，我们就可以得到一个更有意思的下划线 hover 效果</p>
<p>CSS 代码示意，注意看两条使用 background 模拟的下划线的 <code>background-position</code> 的值是不一样的：</p>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-tag">a</span> &#123;</span><br><span class="line">  <span class="attribute">background</span>: </span><br><span class="line">   <span class="built_in">linear-gradient</span>(<span class="number">90deg</span>,<span class="number">#0cc</span>,<span class="number">#0cc</span>),</span><br><span class="line">   <span class="built_in">linear-gradient</span>(<span class="number">90deg</span>, <span class="number">#ff3c41</span>, <span class="number">#fc0</span>, <span class="number">#0ebeff</span>);</span><br><span class="line">  <span class="attribute">background-size</span>: <span class="number">100%</span> <span class="number">3px</span>, <span class="number">0</span> <span class="number">3px</span>;</span><br><span class="line">  <span class="attribute">background-repeat</span>: no-repeat;</span><br><span class="line"> <span class="attribute">background-position</span>: <span class="number">100%</span> <span class="number">100%</span>, <span class="number">0</span> <span class="number">100%</span>;</span><br><span class="line">  <span class="attribute">transition</span>: all .<span class="number">5s</span>;</span><br><span class="line">  <span class="attribute">color</span>: <span class="number">#0cc</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="selector-tag">a</span><span class="selector-pseudo">:hover</span> &#123;</span><br><span class="line">  <span class="attribute">background-size</span>: <span class="number">0</span> <span class="number">3px</span>, <span class="number">100%</span> <span class="number">3px</span>;</span><br><span class="line">  <span class="attribute">color</span>: <span class="number">#000</span>;</span><br><span class="line">  <span class="comment">/* 4.0</span></span><br><span class="line"><span class="comment">  background-size: 100% 3px;</span></span><br><span class="line"><span class="comment">  color: #000; */</span></span><br><span class="line"> &#125;</span><br></pre></td></tr></table></figure>

<p>可以得到这样一种效果，其实每次 hover， 都有两条下划线在移动：</p>
<p><img src="https://cdn.jsdelivr.net/gh/Vixcity/FigureBed/img/202109161131066.gif" alt="图片"></p>
<p><a target="_blank" rel="noopener" href="https://codepen.io/Chokcoco/pen/MWJogjQ">background underline animation</a></p>
<h2 id="通过-background-size-与-background-position-配合-background-clip-实现文字的渐现"><a href="#通过-background-size-与-background-position-配合-background-clip-实现文字的渐现" class="headerlink" title="通过 background-size 与 background-position 配合 background-clip 实现文字的渐现"></a>通过 <code>background-size</code> 与 <code>background-position</code> 配合 <code>background-clip</code> 实现文字的渐现</h2><p>上述一大段都在围绕 – <strong>下划线</strong>。</p>
<p>回归到本文一开始提到的 Gif 效果，我们能否实现在一段文字中，实现文字的渐现效果呢？</p>
<p>上述技巧利用的是 <code>background</code>，那么 <code>background</code> 背景色能否改变文字的颜色的？答案是可以的，只需要借助 <code>background-clip</code>。</p>
<p>我们稍微改造下代码，实现利用 <code>background-clip</code> 实现 hover 的时候部分文字逐渐改变颜色：</p>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-tag">p</span> &#123;  </span><br><span class="line">    <span class="attribute">color</span>: <span class="number">#666</span>;  </span><br><span class="line">    <span class="attribute">cursor</span>: pointer;  </span><br><span class="line">&#125;  </span><br><span class="line">  </span><br><span class="line"><span class="selector-tag">a</span> &#123;      </span><br><span class="line">  <span class="attribute">background</span>: <span class="built_in">linear-gradient</span>(<span class="number">90deg</span>,<span class="number">#fc0</span>,<span class="number">#fc0</span>);</span><br><span class="line">  <span class="attribute">background-size</span>: <span class="number">0</span> <span class="number">100px</span>;</span><br><span class="line">  <span class="attribute">background-repeat</span>: no-repeat;</span><br><span class="line">  <span class="attribute">background-position</span>: <span class="number">0</span> <span class="number">100%</span>;</span><br><span class="line">  -webkit-<span class="attribute">background-clip</span>: text;</span><br><span class="line">  <span class="attribute">transition</span>: all .<span class="number">6s</span> linear;</span><br><span class="line">&#125;  </span><br><span class="line">  </span><br><span class="line"><span class="selector-tag">p</span><span class="selector-pseudo">:hover</span> &#123;</span><br><span class="line"> <span class="attribute">background-size</span>: <span class="number">100%</span> <span class="number">100%</span>;</span><br><span class="line"> <span class="attribute">color</span>: transparent;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>看看效果，通过 <code>background-clip: text</code> 的遮罩裁剪，我们将 <code>background: linear-gradient(90deg, #fc0, #fc0)</code> 背景色作用给了文字，同时利用 <code>color: transparent</code> 让文字展示出背景色的色值：</p>
<p><img src="https://cdn.jsdelivr.net/gh/Vixcity/FigureBed/img/202109161215600.gif" alt="图片"></p>
<p><a target="_blank" rel="noopener" href="https://codepen.io/Chokcoco/pen/qBjmvdq?editors=1100">background-size 与 background-position 以及 background-clip 实现文字逐个渐现</a></p>
<p>当然，稍微对上述代码变形，我们就可以演化出几种不同的效果。</p>
<h3 id="实现整段文字的渐现-从透明到出现"><a href="#实现整段文字的渐现-从透明到出现" class="headerlink" title="实现整段文字的渐现 - 从透明到出现"></a>实现整段文字的渐现 - 从透明到出现</h3><p>第一种就是从透明到有颜色，逐渐展现，这里我们只需要让 color 一直是 transparent 即可（下述效果借助了一个按钮去触发效果）：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;button&quot;</span>&gt;</span>Button<span class="tag">&lt;/<span class="name">div</span>&gt;</span> </span><br><span class="line"><span class="comment">&lt;!-- &lt;p&gt;</span></span><br><span class="line"><span class="comment">	测试测试测试测试测试测试测试测试测试测试测试测试测试测试&lt;a&gt;测试测试测试测测试测试测试测试测试测试测试测试测试测试测试测试试测试测试&lt;/a&gt;测试测试</span></span><br><span class="line"><span class="comment">&lt;/p&gt; --&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">p</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">a</span>&gt;</span>测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测测试测试测试测试测试测试测试测试测试测试测试测试试测试测试测试测试<span class="tag">&lt;/<span class="name">a</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br></pre></td></tr></table></figure>

<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-tag">a</span> &#123;      </span><br><span class="line">    <span class="attribute">background</span>: <span class="built_in">linear-gradient</span>(<span class="number">90deg</span>, <span class="number">#fc0</span>, <span class="number">#fc0</span>);  </span><br><span class="line">    <span class="attribute">background-size</span>: <span class="number">0</span> <span class="number">100px</span>;  </span><br><span class="line">    <span class="attribute">background-repeat</span>: no-repeat;  </span><br><span class="line">    <span class="attribute">background-position</span>: <span class="number">0</span> <span class="number">100%</span>;  </span><br><span class="line">    <span class="attribute">color</span>: transparent;  </span><br><span class="line">    <span class="attribute">background-clip</span>: text;  </span><br><span class="line">&#125;  </span><br><span class="line"><span class="selector-class">.button</span><span class="selector-pseudo">:hover</span> ~ <span class="selector-tag">p</span> <span class="selector-tag">a</span> &#123;  </span><br><span class="line">    <span class="attribute">transition</span>: .<span class="number">8s</span> all linear;  </span><br><span class="line">    <span class="attribute">background-size</span>: <span class="number">100%</span> <span class="number">100%</span>;  </span><br><span class="line">&#125;  </span><br></pre></td></tr></table></figure>

<p>效果如下：</p>
<p><img src="https://cdn.jsdelivr.net/gh/Vixcity/FigureBed/img/202109161217034.gif" alt="图片"></p>
<h3 id="实现整段文字的渐现-从一种颜色到另外一种颜色"><a href="#实现整段文字的渐现-从一种颜色到另外一种颜色" class="headerlink" title="实现整段文字的渐现 - 从一种颜色到另外一种颜色"></a>实现整段文字的渐现 - 从一种颜色到另外一种颜色</h3><p>还可以实现文字从一种颜色到另外一种颜色的逐个转变，只需要添加多一层 <code>background-image</code> 渐变。</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;button&quot;</span>&gt;</span>Button<span class="tag">&lt;/<span class="name">div</span>&gt;</span>  </span><br><span class="line"><span class="tag">&lt;<span class="name">p</span>&gt;</span><span class="tag">&lt;<span class="name">a</span>&gt;</span>Lorem ipsum dolor sit amet consectetur adipisicing elit. Mollitia nostrum placeat consequatur deserunt velit ducimus possimus commodi temporibus debitis quam, molestiae laboriosam sit repellendus sed sapiente quidem quod accusantium vero.<span class="tag">&lt;/<span class="name">a</span>&gt;</span><span class="tag">&lt;/<span class="name">p</span>&gt;</span>  </span><br></pre></td></tr></table></figure>

<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-tag">a</span> &#123;      </span><br><span class="line"> <span class="attribute">background</span>: </span><br><span class="line">   <span class="built_in">linear-gradient</span>(<span class="number">90deg</span>, <span class="number">#999</span>, <span class="number">#999</span>),</span><br><span class="line">   <span class="built_in">linear-gradient</span>(<span class="number">90deg</span>, <span class="number">#fc0</span>, <span class="number">#fc0</span>);</span><br><span class="line">  <span class="attribute">background-size</span>: <span class="number">100%</span> <span class="number">100%</span>, <span class="number">0</span> <span class="number">100px</span>;</span><br><span class="line">  <span class="attribute">background-repeat</span>: no-repeat;</span><br><span class="line">  <span class="attribute">background-position</span>: <span class="number">100%</span> <span class="number">100%</span>, <span class="number">0</span> <span class="number">100%</span>;</span><br><span class="line">  <span class="attribute">color</span>: transparent;</span><br><span class="line">  -webkit-<span class="attribute">background-clip</span>: text; </span><br><span class="line">&#125;  </span><br><span class="line"><span class="selector-class">.button</span><span class="selector-pseudo">:hover</span> ~ <span class="selector-tag">p</span> <span class="selector-tag">a</span> &#123;  </span><br><span class="line"> <span class="attribute">transition</span>: all .<span class="number">8s</span> linear;</span><br><span class="line"> <span class="attribute">background-size</span>: <span class="number">0</span> <span class="number">100px</span>, <span class="number">100%</span> <span class="number">100%</span>;</span><br><span class="line">&#125;  </span><br></pre></td></tr></table></figure>

<p>这里需要解释一下，虽然设置了 <code>color: transparent</code>，但是文字默认还是有颜色的，默认的文字颜色，是由第一层渐变赋予的 <code>background: linear-gradient(90deg, #999, #999), linear-gradient(90deg, #fc0, #fc0)</code>，也就是这一层：<code>linear-gradient(90deg, #999, #999)</code>。</p>
<p><img src="https://cdn.jsdelivr.net/gh/Vixcity/FigureBed/img/202109161240771.gif" alt="图片"></p>
<p>当 hover 触发时，<code>linear-gradient(90deg, #999, #999)</code> 这一层渐变逐渐消失</p>
<p>而另外一层 <code>linear-gradient(90deg, #fc0, #fc0)</code> 逐渐出现，借此实现上述效果</p>
<p><a target="_blank" rel="noopener" href="https://codepen.io/Chokcoco/pen/XWgpyqz">background-clip 文字渐现效果</a></p>
<h2 id="简单模拟题图效果"><a href="#简单模拟题图效果" class="headerlink" title="简单模拟题图效果"></a>简单模拟题图效果</h2><p>这里，我们简单利用这个技巧模拟一下文章一开始列出的 Gif 的效果：</p>
<p><img src="https://cdn.jsdelivr.net/gh/Vixcity/FigureBed/img/202109161242903.gif" alt="图片"></p>
<p>这个效果原作者的技巧是：  </p>
<ol>
<li><p>将每个单词作为一个对象，包裹一个特殊的 class</p>
</li>
<li><p>利用 <code>animation-delay</code> 将动画逐渐赋予每个单词</p>
</li>
</ol>
<p>这里，我们将整段文本统一处理，简单还原：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;button&quot;</span>&gt;</span>Button<span class="tag">&lt;/<span class="name">div</span>&gt;</span>  </span><br><span class="line"><span class="tag">&lt;<span class="name">p</span>&gt;</span><span class="tag">&lt;<span class="name">a</span>&gt;</span>Lorem ipsum dolor sit amet consectetur adipisicing elit. Mollitia nostrum placeat consequatur deserunt velit ducimus possimus commodi temporibus debitis quam, molestiae laboriosam sit repellendus sed sapiente quidem quod accusantium vero.<span class="tag">&lt;/<span class="name">a</span>&gt;</span><span class="tag">&lt;/<span class="name">p</span>&gt;</span>  </span><br></pre></td></tr></table></figure>

<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/** 动画核心 background、line-height、opacity **/</span>  </span><br><span class="line"><span class="selector-tag">a</span> &#123;      </span><br><span class="line">    <span class="attribute">background</span>:   </span><br><span class="line">        <span class="built_in">linear-gradient</span>(<span class="number">90deg</span>, <span class="number">#ff5722</span>, <span class="number">#ff5722</span>),  </span><br><span class="line">        <span class="built_in">linear-gradient</span>(<span class="number">90deg</span>, <span class="number">#aaa</span>, <span class="number">#aaa</span>);  </span><br><span class="line">    <span class="attribute">background-size</span>: <span class="number">100%</span> <span class="number">100%</span>, <span class="number">0</span> <span class="number">100px</span>;  </span><br><span class="line">    <span class="attribute">background-repeat</span>: no-repeat;  </span><br><span class="line">    <span class="attribute">background-position</span>: <span class="number">100%</span> <span class="number">100%</span>, <span class="number">0</span> <span class="number">100%</span>;  </span><br><span class="line">    <span class="attribute">cursor</span>: pointer;  </span><br><span class="line">    <span class="attribute">color</span>: transparent;  </span><br><span class="line">    <span class="attribute">background-clip</span>: text;  </span><br><span class="line">    <span class="attribute">line-height</span>: <span class="number">3</span>;  </span><br><span class="line">    <span class="attribute">opacity</span>: <span class="number">0</span>;  </span><br><span class="line">&#125;  </span><br><span class="line"><span class="selector-class">.button</span><span class="selector-pseudo">:hover</span> ~ <span class="selector-tag">p</span> <span class="selector-tag">a</span> &#123;  </span><br><span class="line">    <span class="attribute">transition</span>: <span class="number">1.2s</span> background .<span class="number">3s</span> ease-out, .<span class="number">8s</span> line-height ease-out, .<span class="number">6s</span> opacity ease-in;  </span><br><span class="line">    <span class="attribute">background-size</span>: <span class="number">0</span> <span class="number">100px</span>, <span class="number">100%</span> <span class="number">100%</span>;  </span><br><span class="line">    <span class="attribute">color</span>: transparent;  </span><br><span class="line">    <span class="attribute">line-height</span>: <span class="number">1</span>;  </span><br><span class="line">    <span class="attribute">opacity</span>: <span class="number">1</span>;  </span><br><span class="line">&#125;  </span><br><span class="line">/ ** 简单控制半透明黑色遮罩出现 **/  </span><br><span class="line"><span class="selector-tag">a</span><span class="selector-pseudo">::before</span> &#123;  </span><br><span class="line">    <span class="attribute">content</span>: <span class="string">&quot;&quot;</span>;  </span><br><span class="line">    <span class="attribute">position</span>: fixed;  </span><br><span class="line">    <span class="attribute">background</span>: <span class="built_in">rgba</span>(<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, .<span class="number">8</span>);  </span><br><span class="line">    <span class="attribute">top</span>: <span class="number">0</span>;  </span><br><span class="line">    <span class="attribute">left</span>: <span class="number">0</span>;  </span><br><span class="line">    <span class="attribute">right</span>: <span class="number">0</span>;  </span><br><span class="line">    <span class="attribute">bottom</span>: <span class="number">0</span>;  </span><br><span class="line">    <span class="attribute">z-index</span>: -<span class="number">1</span>;  </span><br><span class="line">    <span class="attribute">transition</span>: .<span class="number">3s</span> all linear;  </span><br><span class="line">    <span class="attribute">opacity</span>: <span class="number">0</span>;  </span><br><span class="line">&#125;  </span><br><span class="line"><span class="selector-class">.button</span><span class="selector-pseudo">:hover</span> ~ <span class="selector-tag">p</span> <span class="selector-tag">a</span><span class="selector-pseudo">::before</span> &#123;  </span><br><span class="line">    <span class="attribute">opacity</span>: <span class="number">1</span>;  </span><br><span class="line">&#125;  </span><br></pre></td></tr></table></figure>

<p>效果如下：</p>
<p><img src="https://cdn.jsdelivr.net/gh/Vixcity/FigureBed/img/202109161252993.gif" alt="图片"></p>
<p>可以看到，由于是整体控制整段文本，效果上没有逐个单词控制的好，但是优点是代码量非常少。对于一些简单卡片类的 hover 场景，足以。  </p>
<p><a target="_blank" rel="noopener" href="https://codepen.io/Chokcoco/pen/abwWMJm">background-image、background-clip 实现文字渐现效果</a></p>
<h2 id="完美还原题图效果"><a href="#完美还原题图效果" class="headerlink" title="完美还原题图效果"></a>完美还原题图效果</h2><p>当然，题图效果使用纯 CSS 也是不在话下的。只不过就不是简单能够统一处理的了。</p>
<p>这里，我们需要对每一个单词进行精细化的处理，并且使用每个单词的伪元素进行额外的动画。</p>
<p>简单的结构如下：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;button&quot;</span>&gt;</span>Button<span class="tag">&lt;/<span class="name">div</span>&gt;</span>  </span><br><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;g-wrap&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span>  </span><br><span class="line"><span class="tag">&lt;<span class="name">p</span>&gt;</span>  </span><br><span class="line">    <span class="tag">&lt;<span class="name">span</span> <span class="attr">data-text</span>=<span class="string">&quot;Lorem&quot;</span>&gt;</span>Lorem<span class="tag">&lt;/<span class="name">span</span>&gt;</span>   </span><br><span class="line">    <span class="tag">&lt;<span class="name">span</span> <span class="attr">data-text</span>=<span class="string">&quot;ipsum&quot;</span>&gt;</span>ipsum<span class="tag">&lt;/<span class="name">span</span>&gt;</span>   </span><br><span class="line">    <span class="tag">&lt;<span class="name">span</span> <span class="attr">data-text</span>=<span class="string">&quot;dolor&quot;</span>&gt;</span>dolor<span class="tag">&lt;/<span class="name">span</span>&gt;</span>   </span><br><span class="line">    <span class="tag">&lt;<span class="name">span</span> <span class="attr">data-text</span>=<span class="string">&quot;sit&quot;</span>&gt;</span>sit<span class="tag">&lt;/<span class="name">span</span>&gt;</span>   </span><br><span class="line">    <span class="tag">&lt;<span class="name">span</span> <span class="attr">data-text</span>=<span class="string">&quot;amet&quot;</span>&gt;</span>amet<span class="tag">&lt;/<span class="name">span</span>&gt;</span>   </span><br><span class="line">    // ... 类似结构       </span><br><span class="line"><span class="tag">&lt;/<span class="name">p</span>&gt;</span>  </span><br></pre></td></tr></table></figure>

<p>可以看到，每个单词都被 <code>&lt;span&gt;</code> 包裹，并且添加了 <code>data-text</code>，方便伪元素拿到当前单词。</p>
<p>接下来，就是设定动画，并且通过顺序给每个 <code>&lt;span&gt;</code> 添加相应递增的 <code>animation-delay</code> 以实现没个单词动画的差异性。核心的伪代码如下：</p>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-tag">p</span> &#123;  </span><br><span class="line">    <span class="attribute">position</span>: relative;  </span><br><span class="line">    <span class="attribute">width</span>: <span class="number">500px</span>;  </span><br><span class="line">    <span class="attribute">overflow</span>: hidden;  </span><br><span class="line">&#125;  </span><br><span class="line">  </span><br><span class="line"><span class="selector-tag">p</span> <span class="selector-tag">span</span> &#123;  </span><br><span class="line">    <span class="attribute">position</span>: relative;  </span><br><span class="line">    <span class="attribute">display</span>: inline-block;  </span><br><span class="line">    <span class="attribute">opacity</span>: <span class="number">0</span>;  </span><br><span class="line">    <span class="attribute">transform</span>: <span class="built_in">translateY</span>(<span class="number">15px</span>) <span class="built_in">translateZ</span>(<span class="number">0</span>);  </span><br><span class="line">    <span class="attribute">transition-property</span>: transform, opacity;  </span><br><span class="line">    <span class="attribute">transition-duration</span>: .<span class="number">3s</span>, .<span class="number">2s</span>;  </span><br><span class="line">&#125;  </span><br><span class="line">  </span><br><span class="line"><span class="selector-class">.button</span><span class="selector-pseudo">:hover</span> ~ <span class="selector-tag">p</span> <span class="selector-tag">span</span> &#123;  </span><br><span class="line">    <span class="attribute">opacity</span>: <span class="number">1</span>;  </span><br><span class="line">    <span class="attribute">color</span>: <span class="number">#ddd</span>;  </span><br><span class="line">    <span class="attribute">transform</span>: <span class="built_in">translateY</span>(<span class="number">0</span>) <span class="built_in">translateZ</span>(<span class="number">0</span>);  </span><br><span class="line">    <span class="attribute">transition-duration</span>: <span class="number">1s</span>, .<span class="number">2s</span>;  </span><br><span class="line">&#125;  </span><br><span class="line">  </span><br><span class="line"><span class="selector-tag">p</span> <span class="selector-tag">span</span>:after,  </span><br><span class="line">p span:before &#123;  </span><br><span class="line">    position: absolute;  </span><br><span class="line">    <span class="attribute">content</span>: <span class="built_in">attr</span>(data-text);  </span><br><span class="line">    <span class="attribute">top</span>: <span class="number">0</span>;  </span><br><span class="line">    <span class="attribute">left</span>: <span class="number">0</span>;  </span><br><span class="line">    <span class="attribute">z-index</span>: <span class="number">1</span>;  </span><br><span class="line">    <span class="attribute">transform</span>: <span class="built_in">translateZ</span>(<span class="number">0</span>);  </span><br><span class="line">&#125;  </span><br><span class="line">  </span><br><span class="line"><span class="selector-tag">p</span> <span class="selector-tag">span</span>:after &#123;  </span><br><span class="line">    color: <span class="number">#e62541</span>;  </span><br><span class="line">    <span class="attribute">transition-property</span>: opacity;  </span><br><span class="line">    <span class="attribute">transition-duration</span>: .<span class="number">1s</span>;  </span><br><span class="line">&#125;  </span><br><span class="line">  </span><br><span class="line"><span class="selector-class">.button</span><span class="selector-pseudo">:hover</span> ~ <span class="selector-tag">p</span> <span class="selector-tag">span</span>:after &#123;  </span><br><span class="line">    opacity: <span class="number">0</span>;  </span><br><span class="line">    <span class="attribute">transition-property</span>: opacity;  </span><br><span class="line">    <span class="attribute">transition-duration</span>: .<span class="number">4s</span>;  </span><br><span class="line">&#125;  </span><br><span class="line">  </span><br><span class="line"><span class="keyword">@for</span> $i from <span class="number">1</span> to <span class="number">21</span> &#123;  </span><br><span class="line">    <span class="selector-tag">p</span> <span class="selector-tag">span</span><span class="selector-pseudo">:nth-child</span>(#&#123;$<span class="selector-tag">i</span>&#125;) &#123;  </span><br><span class="line">        <span class="attribute">transition-delay</span>: #&#123;$i * <span class="number">0.04</span>&#125;s;  </span><br><span class="line">          </span><br><span class="line">        &amp;<span class="selector-pseudo">::after</span> &#123;  </span><br><span class="line">            <span class="attribute">transition-delay</span>: #&#123;$i * <span class="number">0.04</span> + <span class="number">0.2</span>&#125;s;  </span><br><span class="line">        &#125;  </span><br><span class="line">    &#125;  </span><br><span class="line">&#125;  </span><br></pre></td></tr></table></figure>

<p>其实动画本身不太复杂，主要讲两点：</p>
<ol>
<li>hover 状态下和非 hover 状态下的 <code>transition-duration</code> 是不一样的，是因为取消 hover 过程中，动画消失过程的时间通常是要求更短的；</li>
<li>借助了 SASS 的循环 <code>@for $i from 1 to 21 &#123;&#125;</code> 递增给每个 <code>span</code> 和它的伪元素添加了递增的 <code>transition-delay</code>；</li>
</ol>
<p>最终，我们可以得到如下的结果：</p>
<p><img src="https://cdn.jsdelivr.net/gh/Vixcity/FigureBed/img/202109161251075.gif" alt="图片"></p>
<p>完整的代码，你可以猛戳 – <a target="_blank" rel="noopener" href="https://csscoco.com/inspiration/#/./animation/animation-delay-control-text-effect">CSS 灵感 - 利用 animation-delay 实现文字渐现效果</a></p>
<h2 id="最后"><a href="#最后" class="headerlink" title="最后"></a>最后</h2><p>好了，本文到此结束，希望对你有帮助 :)</p>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="http://vixcity.gitee.io/my-gitee-blog">IQ前端</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="http://vixcity.gitee.io/my-gitee-blog/2021/09/16/%E3%80%90CSS%E3%80%91%E5%A6%99%E7%94%A8%20background%20%E5%AE%9E%E7%8E%B0%E8%8A%B1%E5%BC%8F%E6%96%87%E5%AD%97%E6%95%88%E6%9E%9C/">http://vixcity.gitee.io/my-gitee-blog/2021/09/16/%E3%80%90CSS%E3%80%91%E5%A6%99%E7%94%A8%20background%20%E5%AE%9E%E7%8E%B0%E8%8A%B1%E5%BC%8F%E6%96%87%E5%AD%97%E6%95%88%E6%9E%9C/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">转载自微信公众号《 IQ前端 》如有侵权请联系删除</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/my-gitee-blog/tags/CSS/">CSS</a></div><div class="post_share"><div class="social-share" data-image="/my-gitee-blog/img/CSS.jpg" data-sites="wechat,qq"></div><link rel="stylesheet" href="/my-gitee-blog/" media="print" onload="this.media='all'"><script src="/my-gitee-blog/" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/my-gitee-blog/2021/09/17/%E3%80%90JavaScript%E3%80%9110%E4%B8%AA%E6%B8%85%E6%99%B0%E5%AE%9E%E7%94%A8%E6%9B%B4%E6%98%BE%E4%B8%93%E4%B8%9A%E7%9A%84JavaScript%E4%BB%A3%E7%A0%81%E7%89%87%E6%AE%B5/"><img class="prev-cover" src="/my-gitee-blog/img/JS.jpg" onerror="onerror=null;src='/my-gitee-blog/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">【JavaScript】10个清晰实用更显专业的JavaScript代码片段</div></div></a></div><div class="next-post pull-right"><a href="/my-gitee-blog/2021/09/15/%E3%80%90JavaScript%E3%80%91%E5%87%A0%E4%B8%AAJavaScript%E6%9E%81%E7%9F%AD%E6%97%A5%E6%9C%9F%E6%97%B6%E9%97%B4%E4%BB%A3%E7%A0%81%E7%89%87%E6%AE%B5%EF%BC%88%E4%BD%A0%E5%80%BC%E5%BE%97%E6%8B%A5%E6%9C%89%EF%BC%89/"><img class="next-cover" src="/my-gitee-blog/img/JS.jpg" onerror="onerror=null;src='/my-gitee-blog/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">【JavaScript】几个JavaScript极短日期时间代码片段（你值得拥有）</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/my-gitee-blog/2021/10/04/%E3%80%90CSS%E3%80%91CSS%20%E4%BB%A3%E7%A0%81%E9%87%8D%E6%9E%84%E4%B8%8E%E4%BC%98%E5%8C%96%E4%B9%8B%E8%B7%AF/" title="【CSS】CSS 代码重构与优化之路"><img class="cover" src="/my-gitee-blog/img/CSS.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-10-04</div><div class="title">【CSS】CSS 代码重构与优化之路</div></div></a></div><div><a href="/my-gitee-blog/2021/09/09/%E3%80%90CSS%E3%80%91margin%20%E5%A1%8C%E9%99%B7%E5%92%8C%E5%90%88%E5%B9%B6%E9%97%AE%E9%A2%98/" title="【CSS杂谈】margin 塌陷和合并问题"><img class="cover" src="/my-gitee-blog/img/CSS.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-09-09</div><div class="title">【CSS杂谈】margin 塌陷和合并问题</div></div></a></div><div><a href="/my-gitee-blog/2021/09/13/%E3%80%90CSS%E3%80%91%E5%85%A8%E5%85%BC%E5%AE%B9%E7%9A%84%E6%AF%9B%E7%8E%BB%E7%92%83%E6%95%88%E6%9E%9C/" title="【CSS杂谈】全兼容的毛玻璃效果"><img class="cover" src="/my-gitee-blog/img/CSS.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-09-13</div><div class="title">【CSS杂谈】全兼容的毛玻璃效果</div></div></a></div><div><a href="/my-gitee-blog/2021/09/22/%E3%80%90CSS%E3%80%91%E5%AE%9E%E7%8E%B0%E6%96%B0%E6%8B%9F%E6%80%81(Neumorphism)%20UI%20%E9%A3%8E%E6%A0%BC/" title="【CSS】实现新拟态(Neumorphism) UI 风格"><img class="cover" src="/my-gitee-blog/img/CSS.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-09-22</div><div class="title">【CSS】实现新拟态(Neumorphism) UI 风格</div></div></a></div><div><a href="/my-gitee-blog/2021/10/11/%E3%80%90CSS%E3%80%9150%E4%B8%AA%E6%9C%89%E4%BB%B7%E5%80%BC%E7%9A%84CSS%E7%BC%96%E5%86%99%E8%A7%84%E5%88%99%EF%BC%8C%E8%AE%A9%E4%BD%A0%E5%86%99%E5%87%BA%E6%9B%B4%E5%A5%BD%E7%9A%84CSS/" title="【CSS】50个有价值的CSS编写规则，让你写出更好的CSS"><img class="cover" src="/my-gitee-blog/img/CSS.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-10-11</div><div class="title">【CSS】50个有价值的CSS编写规则，让你写出更好的CSS</div></div></a></div></div></div><hr/><div id="post-comment"><div class="comment-head"><div class="comment-headline"><i class="fas fa-comments fa-fw"></i><span> 评论</span></div></div><div class="comment-wrap"><div><div id="lv-container" data-id="city" data-uid="MTAyMC81NDQ0NS8zMDkxNg=="></div></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/my-gitee-blog/img/avatar.png" onerror="this.onerror=null;this.src='/my-gitee-blog/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">Vixcity</div><div class="author-info__description">今日事，今日毕</div></div><div class="card-info-data"><div class="card-info-data-item is-center"><a href="/my-gitee-blog/archives/"><div class="headline">文章</div><div class="length-num">55</div></a></div><div class="card-info-data-item is-center"><a href="/my-gitee-blog/tags/"><div class="headline">标签</div><div class="length-num">50</div></a></div><div class="card-info-data-item is-center"><a href="/my-gitee-blog/categories/"><div class="headline">分类</div><div class="length-num">18</div></a></div></div><a class="button--animated" id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/Vixcity"><i class="fab fa-github"></i><span>我的Github</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/Vixcity" target="_blank" title="Github"><i class="fab fa-github"></i></a><a class="social-icon" href="mailto:2091283625@qq.com" target="_blank" title="Email"><i class="fas fa-envelope"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">这是Vixcity目前的小博客</div></div><div class="sticky_layout"><div class="card-widget card-history"><div class="card-content"><div class="item-headline"><i class="fas fa-clock fa-spin"></i><span>那年今日</span></div><div id="history-baidu" style="height: 100px;overflow: hidden;"><div class="history_swiper-container" id="history-container" style="width: 100%;height: 100%;"><div class="swiper-wrapper" id="history_container_wrapper" style="height:20px"></div></div></div></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/my-gitee-blog/2022/07/13/%E3%80%90NodeJS%E3%80%91NodeJS%20%E6%9C%8D%E5%8A%A1%E7%AB%AF%E8%AF%B7%E6%B1%82/" title="【NodeJS】NodeJS 服务端请求"><img src="/my-gitee-blog/img/NodeJs.jpg" onerror="this.onerror=null;this.src='/my-gitee-blog/img/404.jpg'" alt="【NodeJS】NodeJS 服务端请求"/></a><div class="content"><a class="title" href="/my-gitee-blog/2022/07/13/%E3%80%90NodeJS%E3%80%91NodeJS%20%E6%9C%8D%E5%8A%A1%E7%AB%AF%E8%AF%B7%E6%B1%82/" title="【NodeJS】NodeJS 服务端请求">【NodeJS】NodeJS 服务端请求</a><time datetime="2022-07-12T16:00:00.000Z" title="发表于 2022-07-13 00:00:00">2022-07-13</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/my-gitee-blog/2022/07/12/%E3%80%90Hexo%E3%80%91%E6%A0%87%E7%AD%BE%E6%A0%B7%E5%BC%8F%E5%88%97%E8%A1%A8%E5%8F%82%E8%80%83%20-%201/" title="【Hexo】标签样式列表参考 - 1"><img src="/my-gitee-blog/img/hexo.jpg" onerror="this.onerror=null;this.src='/my-gitee-blog/img/404.jpg'" alt="【Hexo】标签样式列表参考 - 1"/></a><div class="content"><a class="title" href="/my-gitee-blog/2022/07/12/%E3%80%90Hexo%E3%80%91%E6%A0%87%E7%AD%BE%E6%A0%B7%E5%BC%8F%E5%88%97%E8%A1%A8%E5%8F%82%E8%80%83%20-%201/" title="【Hexo】标签样式列表参考 - 1">【Hexo】标签样式列表参考 - 1</a><time datetime="2022-07-11T16:00:00.000Z" title="发表于 2022-07-12 00:00:00">2022-07-12</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/my-gitee-blog/2022/07/12/%E3%80%90Hexo%E3%80%91%E6%A0%87%E7%AD%BE%E6%A0%B7%E5%BC%8F%E5%88%97%E8%A1%A8%E5%8F%82%E8%80%83%20-%202/" title="【Hexo】标签样式列表参考 - 2"><img src="/my-gitee-blog/img/hexo.jpg" onerror="this.onerror=null;this.src='/my-gitee-blog/img/404.jpg'" alt="【Hexo】标签样式列表参考 - 2"/></a><div class="content"><a class="title" href="/my-gitee-blog/2022/07/12/%E3%80%90Hexo%E3%80%91%E6%A0%87%E7%AD%BE%E6%A0%B7%E5%BC%8F%E5%88%97%E8%A1%A8%E5%8F%82%E8%80%83%20-%202/" title="【Hexo】标签样式列表参考 - 2">【Hexo】标签样式列表参考 - 2</a><time datetime="2022-07-11T16:00:00.000Z" title="发表于 2022-07-12 00:00:00">2022-07-12</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/my-gitee-blog/2022/07/12/%E3%80%90Hexo%E3%80%91%E6%A0%87%E7%AD%BE%E6%A0%B7%E5%BC%8F%E5%88%97%E8%A1%A8%E5%8F%82%E8%80%83%20-%203/" title="【Hexo】标签样式列表参考 - 3"><img src="/my-gitee-blog/img/hexo.jpg" onerror="this.onerror=null;this.src='/my-gitee-blog/img/404.jpg'" alt="【Hexo】标签样式列表参考 - 3"/></a><div class="content"><a class="title" href="/my-gitee-blog/2022/07/12/%E3%80%90Hexo%E3%80%91%E6%A0%87%E7%AD%BE%E6%A0%B7%E5%BC%8F%E5%88%97%E8%A1%A8%E5%8F%82%E8%80%83%20-%203/" title="【Hexo】标签样式列表参考 - 3">【Hexo】标签样式列表参考 - 3</a><time datetime="2022-07-11T16:00:00.000Z" title="发表于 2022-07-12 00:00:00">2022-07-12</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/my-gitee-blog/2022/07/07/%E3%80%90NodeJS%E3%80%91NodeJS%E5%85%A5%E9%97%A8%E5%AD%A6%E4%B9%A0/" title="【NodeJS】NodeJS入门学习"><img src="/my-gitee-blog/img/NodeJs.jpg" onerror="this.onerror=null;this.src='/my-gitee-blog/img/404.jpg'" alt="【NodeJS】NodeJS入门学习"/></a><div class="content"><a class="title" href="/my-gitee-blog/2022/07/07/%E3%80%90NodeJS%E3%80%91NodeJS%E5%85%A5%E9%97%A8%E5%AD%A6%E4%B9%A0/" title="【NodeJS】NodeJS入门学习">【NodeJS】NodeJS入门学习</a><time datetime="2022-07-06T16:00:00.000Z" title="发表于 2022-07-07 00:00:00">2022-07-07</time></div></div></div></div></div></div></main><footer id="footer" style="background-image: url('/my-gitee-blog/img/CSS.jpg')"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2022 By Vixcity</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div><div id="running-time"></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="translateLink" type="button" title="简繁转换">简</button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><a id="to_comment" href="#post-comment" title="直达评论"><i class="fas fa-comments"></i></a><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div></div></div><div id="search-mask"></div></div><div><script src="/my-gitee-blog/js/utils.js"></script><script src="/my-gitee-blog/js/main.js"></script><script src="/my-gitee-blog/js/tw_cn.js"></script><script src="/my-gitee-blog/js/search/local-search.js"></script><script>var preloader = {
  endLoading: () => {
    document.body.style.overflow = 'auto';
    document.getElementById('loading-box').classList.add("loaded")
  },
  initLoading: () => {
    document.body.style.overflow = '';
    document.getElementById('loading-box').classList.remove("loaded")

  }
}
window.addEventListener('load',preloader.endLoading())</script><div class="js-pjax"><script>function loadLivere () {
  if (typeof LivereTower === 'object') {
    window.LivereTower.init()
  }
  else {
    (function(d, s) {
        var j, e = d.getElementsByTagName(s)[0];
        if (typeof LivereTower === 'function') { return; }
        j = d.createElement(s);
        j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
        j.async = true;
        e.parentNode.insertBefore(j, e);
    })(document, 'script');
  }
}

if ('Livere' === 'Livere' || !false) {
  if (false) btf.loadComment(document.getElementById('lv-container'), loadLivere)
  else loadLivere()
}
else {
  function loadOtherComment () {
    loadLivere()
  }
}</script></div><script src="https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js"></script><script src="/my-gitee-blog/js/newStyle.js"></script><script src="/my-gitee-blog/js/sakura.js"></script><script>setInterval(()=>{let create_time=Math.round(new Date("2021-9-8-00:00:00").getTime()/1000);let timestamp=Math.round((new Date().getTime()+8*60*60*1000)/1000);let second=timestamp-create_time;let time=new Array(0,0,0,0,0);if(second>=365*24*3600){time[0]=parseInt(second/(365*24*3600));second%=365*24*3600}if(second>=24*3600){time[1]=parseInt(second/(24*3600));second%=24*3600}if(second>=3600){time[2]=parseInt(second/3600);second%=3600}if(second>=60){time[3]=parseInt(second/60);second%=60}if(second>0){time[4]=second}currentTimeHtml='小博客已经安全运行 '+time[0]+' 年 '+time[1]+' 天 '+time[2]+' 时 '+time[3]+' 分 '+time[4]+' 秒';var elementById=document.getElementById('running-time');if(elementById){elementById.innerHTML=currentTimeHtml}},1000);</script><script src="https://unpkg.com/swiper/swiper-bundle.min.js"></script><script src="https://cdn.jsdelivr.net/gh/Zfour/Butterfly-card-history/baiduhistory/js/main.js"></script><canvas class="fireworks" mobile="false"></canvas><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/fireworks.min.js"></script><script defer="defer" id="fluttering_ribbon" mobile="false" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-fluttering-ribbon.min.js"></script><script id="canvas_nest" defer="defer" color="0,0,255" opacity="0.7" zIndex="-1" count="99" mobile="false" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-nest.min.js"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div><!-- hexo injector body_end start --><script data-pjax>function electric_clock_injector_config(){
                var parent_div_git = document.getElementsByClassName('sticky_layout')[0];
                var item_html = '<div class="card-widget card-clock"><div class="card-glass"><div class="card-background"><div class="card-content"><div id="hexo_electric_clock"><img id="card-clock-loading" src="https://cdn.jsdelivr.net/gh/Zfour/Butterfly-clock/clock/images/weather/loading.gif" style="height: 120px; width: 100%;" data-ll-status="loading" class="entered loading"></div></div></div></div></div>';
                console.log('已挂载electric_clock')
                // parent_div_git.innerHTML=item_html+parent_div_git.innerHTML // 无报错，但不影响使用(支持pjax跳转)
                parent_div_git.insertAdjacentHTML("afterbegin",item_html) // 有报错，但不影响使用(支持pjax跳转)
            }if( document.getElementsByClassName('sticky_layout')[0] && (location.pathname ==='all'|| 'all' ==='all')){

            electric_clock_injector_config()
        } </script><script src="https://pv.sohu.com/cityjson?ie=utf-8"></script><script data-pjax  src="https://cdn.jsdelivr.net/gh/Zfour/hexo-electric-clock@1.0.6/clock.js"></script><div class="js-pjax"><script async="async">var arr = document.getElementsByClassName('recent-post-item');
for(var i = 0;i<arr.length;i++){
    arr[i].classList.add('wow');
    arr[i].classList.add('animate__zoomIn');
    arr[i].setAttribute('data-wow-duration', '1s');
    arr[i].setAttribute('data-wow-delay', '0.5s');
    arr[i].setAttribute('data-wow-offset', '100');
    arr[i].setAttribute('data-wow-iteration', '1');
  }</script><script async="async">var arr = document.getElementsByClassName('card-widget');
for(var i = 0;i<arr.length;i++){
    arr[i].classList.add('wow');
    arr[i].classList.add('animate__zoomIn');
    arr[i].setAttribute('data-wow-duration', '');
    arr[i].setAttribute('data-wow-delay', '');
    arr[i].setAttribute('data-wow-offset', '');
    arr[i].setAttribute('data-wow-iteration', '');
  }</script></div><script defer src="https://unpkg.zhimg.com/hexo-butterfly-wowjs/lib/wow.min.js"></script><script defer src="https://unpkg.zhimg.com/hexo-butterfly-wowjs/lib/wow_init.js"></script><!-- hexo injector body_end end --><script src="/my-gitee-blog/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"log":false,"pluginJsPath":"lib/","pluginModelPath":"assets/","pluginRootPath":"live2dw/","tagMode":false});</script></body></html>